<template>
  <div class="pay-wrapper">
    <a-spin :spinning="loading">
      <a-space direction="vertical" size="large" style="width:100%">
        <a-card :bordered="false" title="订单信息">
          <a-row type="flex" align="middle">
            <a-col :span="12">
              <div>订单号：{{orderInfo.order_no}}</div>
              <div>订单时间：{{orderInfo.created_at}}</div>
            </a-col>
            <a-col :span="12" style="text-align: right">
              <div>应付金额：<span class="txt">￥{{orderInfo.total_fee}}</span></div>
            </a-col>
          </a-row>
        </a-card>
        <a-card :bordered="false" title="支付平台">
          <a-radio-group size="large" @change="onChange">
            <a-radio-button value="wepay">微信</a-radio-button>
            <a-radio-button disabled value="alipay">支付宝</a-radio-button>
          </a-radio-group>
          <div v-if="codeVisible" style="text-align: center">
            <img :src="codeUrl" />
            <p style="font-size:18px">商品名称：{{orderInfo.good_name}} | 付款金额：<span class="txt">￥{{orderInfo.total_fee}}</span></p>
          </div>
        </a-card>
      </a-space>
    </a-spin>
  </div>
</template>

<script>
import apis from '@/api'
import { getOrder, getPayCallback } from '@/api/order'

export default {
  name: 'Pay',
  data () {
    return {
      orderInfo: {},
      loading: false,
      codeUrl: '',
      codeVisible: false,
      timer: null
    }
  },
  created () {
    this.getOrder()
  },
  // destroyed () {
  //   clearInterval(this.timer)
  // },
  methods: {
    getOrder () {
      this.loading = true
      getOrder(this.$route.params.no).then(res => {
        if (res.status === 0) {
          this.orderInfo = res.result
          this.loading = false
        }
      })
    },
    onChange (value) {
      this.codeUrl = process.env.VUE_APP_API_BASE_URL + apis.GetPayWxCode + '?order_no=' + this.$route.params.no
      this.codeVisible = true
      this.timer = setInterval(() => {
        getPayCallback(this.$route.params.no).then(res => {
          if (res.status === 0) {
            clearInterval(this.timer)
            this.$router.push({ name: 'OrderList' })
          }
        })
      }, 2000)
    }
  }
}
</script>

<style scoped>
  .pay-wrapper {
    width: 1190px;
    margin: 0 auto;
  }
  .txt {
    font-size: 24px;
    color: #ff3c28;
  }
</style>
